package com.uenpay.im.ui.old.activity.bankcard;

import android.text.TextUtils;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.invokeitems.HttpPostRequest;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @date 2016-6-12 下午3:57:44
 * @version 1.0
 * @function 银行卡管理
 * 
 */
public class BankCardHandle {

	public interface IBankCardListener {
		void onBankCardResult(int status, String msg);
	}

	/**
	 * 添加银行卡
	 *
	 * @param url
	 * @param dataJson
	 * @param mBankCardListener
	 */
	public static void addBankCard(String url, String dataJson,
								   final IBankCardListener mBankCardListener) {
		HttpPostRequest.onRequest(url, dataJson, HttpPostRequest.REQUESTMETHOD_POST, new HttpPostRequest.IRequest() {
			@Override
			public void onResult(int responseCode, String response) {
				if (responseCode == 200) {
					mBankCardListener.onBankCardResult(0, "");
				} else {
					try {
						JSONObject json = new JSONObject(response);
						mBankCardListener.onBankCardResult(json.optInt("err_code"), json.optString("err_msg"));
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		});

	}

	/**
	 * 验证银行卡
	 * 
	 * @param bankcard
	 * @param accountname
	 * @param mBankCardListener
	 */
	public static void checkBankCard(String bankcard, String accountname,
			final IBankCardListener mBankCardListener) {
		CheckBankCardInvokeItem item = new CheckBankCardInvokeItem(bankcard,
				accountname);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							CheckBankCardInvokeItem.CheckBankCardInvokeItemResult result = ((CheckBankCardInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mBankCardListener.onBankCardResult(
										result.status, result.msg);
							} else {
								mBankCardListener.onBankCardResult(-1, "");
							}
						} else {
							mBankCardListener.onBankCardResult(-1, "");
						}

					}
				});
	}
	
	public interface IGetBankCardListListener{
		void onBankCardList(int status, String msg, ArrayList<String> cards);
	}
	/**
	 * 获取银行卡
	 * @param mIGetBankCardListListener
	 */
	public static void getBankCardList(final IGetBankCardListListener mIGetBankCardListListener) {
		BankCardListInvokeItem item = new BankCardListInvokeItem();
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {
			
			@Override
			public void onResult(Object resultObject, boolean isCache,
					String failReason) {
				if (resultObject != null) {
					BankCardListInvokeItem.BankCardListInvokeItemResult result = ((BankCardListInvokeItem) resultObject)
							.getOutPut();
					if (result != null) {
						mIGetBankCardListListener.onBankCardList(
								result.status, result.msg,result.cardList);
					} else {
						mIGetBankCardListListener.onBankCardList(-1, "",null);
					}
				} else {
					mIGetBankCardListListener.onBankCardList(-1, "",null);
				}
				
			}
		});
	}


	/**
	 * 删除银行卡
	 *
	 * @param url
	 * @param mBankCardListener
	 */
	public static void deleteBankCard(String url,
									  final IBankCardListener mBankCardListener) {
		HttpPostRequest.onRequest(url, "", HttpPostRequest.REQUESTMETHOD_DELETE, new HttpPostRequest.IRequest() {
			@Override
			public void onResult(int responseCode, String response) {
				if (!TextUtils.isEmpty(response)) {
					try {
						JSONObject json = new JSONObject(response);
						mBankCardListener.onBankCardResult(json.optInt("err_code"), json.optString("err_msg"));
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

			}
		});
	}

	public interface IMyBankCardListener {
		void onMyBankCardResult(int status, String msg, ArrayList<BankEntity> arrayList);
	}

	/**
	 * 我的银行卡
	 *
	 * @param mIMyBankCardListener
	 */
	public static void myBankCard(final IMyBankCardListener mIMyBankCardListener) {
		MyBankCardNumInvokeItem item = new MyBankCardNumInvokeItem();
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							MyBankCardNumInvokeItem.MyBankCardNumInvokeItemResult result = ((MyBankCardNumInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIMyBankCardListener.onMyBankCardResult(
										result.status, result.msg, result.arrayList);
							} else {
								mIMyBankCardListener.onMyBankCardResult(-1, failReason, null);
							}
						} else {
							mIMyBankCardListener.onMyBankCardResult(-1, failReason, null);
						}

					}
				});
	}
}
